package com.mxfcode.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mxfcode.back.mapper.BackResourceMapper;
import com.mxfcode.back.mapper.FileMapper;
import com.mxfcode.back.service.BackResourceService;
import com.mxfcode.bbs_common.date.DateUtils;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.common.HostHolder;
import com.mxfcode.entity.BBSResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 ResourceServiceImpl
 * @描述 TODO
 * @作者 mxf
 * @创建日期 2021-03-30 17:43
 **/
@Service
public class BackResourceServiceImpl implements BackResourceService {
    private static final Logger logger = LoggerFactory.getLogger(BackResourceServiceImpl.class);

    @Autowired
    private BackResourceMapper resourceMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private HostHolder hostHolder;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> addResource(BBSResource resource, String files) {

        Map<String,Object> data = new HashMap();

        resource.setStatus(1);
        //默认字段
        resource.setCreateDate(DateUtils.getCurrentDateToDate());
        resource.setCreateBy(hostHolder.getUser().getUserName());
        resource.setCreateName(hostHolder.getUser().getRealName());
        resource.setUpdateDate(DateUtils.getCurrentDateToDate());
        resource.setUpdateBy(hostHolder.getUser().getUserName());
        resource.setUpdateName(hostHolder.getUser().getRealName());
        resource.setUserId(hostHolder.getUser().getId());
        try {
            Integer rows = resourceMapper.addResource(resource);
            Integer resourceId = resource.getId();
            int updateRows = 0;
            if(rows != null){
                updateRows = fileMapper.updateFiles(files,resourceId);
            }
            if(updateRows == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("资源[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("资源[新增]，结果=新增成功！");
        }catch (Exception e){
            logger.error("资源[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, Object> updateResource(BBSResource resource, String files) {
        Map<String,Object> data = new HashMap();
        try{
            resource.setUpdateName(hostHolder.getUser().getRealName());
            resource.setUpdateBy(hostHolder.getUser().getUserName());
            resource.setUpdateDate(DateUtils.getCurrentDateToDate());
            int result = resourceMapper.updateResource(resource);
            //先删掉出本资源的文件列表
            fileMapper.deleteFileByResource(resource.getId(),files);
            //再添加新文件进列表
            fileMapper.updateFiles(files,resource.getId());
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("资源[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("资源[更新]，结果=更新成功！");
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("资源[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public PageDataResult getResourceList(Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        String userId = hostHolder.getUser().getId();
        List<BBSResource> resourceList = resourceMapper.getResourceList(userId);

        PageHelper.startPage(pageNum, pageSize);

        if(resourceList.size() != 0){
            PageInfo<BBSResource> pageInfo = new PageInfo<>(resourceList);
            pageDataResult.setList(resourceList);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public BBSResource getResourceById(Integer id) {
        return resourceMapper.getResourceById(id);
    }

    @Override
    public Map<String, Object> del(Integer id) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除资源
            int result = resourceMapper.del(id);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除资源失败");
                logger.error("删除资源失败");
                return data;
            }
            //删除文件
            result = fileMapper.delByResourceId(id);
            data.put("code",1);
            data.put("msg","删除资源成功");
            logger.info("删除资源成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除资源异常！", e);
        }
        return data;
    }
}
